package com.plian.system.service.imTbjg.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.plian.Tools.*;
import com.plian.Tools.pagedata.PageData;
import com.plian.system.constant.sys.FormStatusConstant;
import com.plian.system.entity.imtbjg.ImProjectInfoTbjg;
import com.plian.system.mapper.ImTbjg.ImProjectInfoTbjgMapper;
import com.plian.system.mybatis.base.BaseFlowService;
import com.plian.system.mybatis.base.BaseServiceImpl;
import com.plian.system.service.imTbjg.IImProjectInfoTbjgService;
import com.plian.system.service.sys.file.FileService;
import com.plian.system.vo.imTbjg.ImProjectInfoTbjgVO;
import com.plian.system.wrapper.imTbjg.ImProjectInfoTbjgWrapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.*;
import static com.plian.system.constant.sys.FormStatusConstant.UNREVIEWED;
import static com.plian.system.constant.sys.FormTypeConstant.PROJECT_INFO_TBJG;

/**
 * 服务实现类
 *
 * @author
 * @since 2019-09-27
 */
@Slf4j
@Service
@AllArgsConstructor
public class ImProjectInfoTbjgServiceImpl extends BaseServiceImpl<ImProjectInfoTbjgMapper, ImProjectInfoTbjg> implements IImProjectInfoTbjgService, BaseFlowService {


	private ImProjectInfoTbjgWrapper imProjectInfoTbjgWrapper;
	private FileService fileService;



	private static Map<String, String> fileNameMap = new HashMap<>();
	@Override
	@Transactional(rollbackFor=RuntimeException.class)
	public boolean saveOrUpdate(ImProjectInfoTbjg imProjectInfoTbjg) {
		boolean flag = false;
		if (!Optional.ofNullable(imProjectInfoTbjg.getId()).isPresent()) {
			flag = true;
		}
		if (imProjectInfoTbjg.getFormType() == null){
			imProjectInfoTbjg.setFormType(String.valueOf(PROJECT_INFO_TBJG));
		}
		if (StringUtil.isBlank(imProjectInfoTbjg.getOrderNumber())){
			imProjectInfoTbjg.setOrderNumber("9999");
		}
		boolean result = super.saveOrUpdate(imProjectInfoTbjg);
		if (flag){
			fileService.saveAll(String.valueOf(imProjectInfoTbjg.getId()), imProjectInfoTbjg.getDocList());
		}else {
			fileService.edit(String.valueOf(imProjectInfoTbjg.getId()), imProjectInfoTbjg.getDocList());
		}
		return result;
	}
	@Override
	@Transactional(rollbackFor=RuntimeException.class)
	public boolean deleteLogic(List<Long> ids) {
		for (Long id : ids) {
			ImProjectInfoTbjg imProjectInfoTbjg = getById(id);
			if (imProjectInfoTbjg == null) {
				return false;
			}
		}
		return super.deleteLogic(ids);
	}
	@Override
	public HashMap<String,Object> commit(String id) {
		handleStatus(Long.valueOf(id), FormStatusConstant.FLOW);

		//将需要提交的流程变量返回，用于工作流侧
		return new HashMap<String,Object>();
	}

	@Override
	public Boolean finish(String id) {
//		saveHistory(getById(id));
		return handleStatus(Long.valueOf(id), FormStatusConstant.FINISH);
	}

	@Override
	public Boolean reject(String id, int formState,String comment) {
		return handleStatus(Long.valueOf(id), formState);
	}

	@Override
	public List<PageData> findByIds(List<String> formIds) {
		LambdaQueryWrapper<ImProjectInfoTbjg> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.in(ImProjectInfoTbjg::getId, Func.toLongList(StringUtils.join(formIds, StringPool.COMMA)));
		List<ImProjectInfoTbjg> projectInfos = list(queryWrapper);
		List<ImProjectInfoTbjgVO> projectInfosVOS = imProjectInfoTbjgWrapper.entityToVO(projectInfos);
		List<PageData> pageDataList = new ArrayList<>();
		try{
			if (CollectionUtil.isNotEmpty(projectInfosVOS)){
				for (ImProjectInfoTbjgVO vo : projectInfosVOS){
					PageData pd = new PageData();
					pd.put("id", vo.getId());
					pd.put("UNITNAME", vo.getFillingUnitName());
					pd.put("APPLICANTNAME", vo.getCreateUserName());
					pd.put("applicantId", vo.getCreateUser());
					pd.put("applyOrganizationId", vo.getFillingUnitId());
					pd.put("formType", PROJECT_INFO_TBJG);
					pd.put("orgName", vo.getFillingUnitName());
					pageDataList.add(pd);
				}
			}

			return pageDataList;
		} catch (Exception e) {
			log.error(ExceptionUtils.getFullStackTrace(e));
		}
		return pageDataList;
	}

	@Override
	public List<Integer> getFormType() {
		return Arrays.asList(PROJECT_INFO_TBJG);
	}

	@Override
	public void goBackToStash(String formId) {
		handleStatus(Long.valueOf(formId), UNREVIEWED);
	}

	private Boolean handleStatus(Long id, int updateStatus) {
		try {
			ImProjectInfoTbjg imProjectInfoTbjg = getById(id);
			/**
			 * 设置审批人和审批时间
			 */
			if (updateStatus == FormStatusConstant.FINISH){
				imProjectInfoTbjg.setApprovedUser(TokenUtil.getTokenUserId());
				Date now = DateUtil.now();
				imProjectInfoTbjg.setApprovedTime(now);
			}
			imProjectInfoTbjg.setStatus(updateStatus);
			updateById(imProjectInfoTbjg);
			return true;
		} catch (Exception e) {
			log.error(ExceptionUtils.getFullStackTrace(e));
			return false;
		}
	}

	@Override
	public boolean submit(ImProjectInfoTbjg imProjectInfoTbjg) {

		boolean result = saveOrUpdate(imProjectInfoTbjg);
		return result;
	}


}
